package com.jubaopen.service;

import com.jubaopen.bean.SmsCharge;
import com.jubaopen.dao.SmsChargeDAO;

public class SmsChargeService {

	private SmsChargeDAO smsChargeDAO;

	public void setSmsChargeDAO(SmsChargeDAO smsChargeDAO) {
		this.smsChargeDAO = smsChargeDAO;
	}
	
	public SmsCharge queryByUserId(int userId) {
		return smsChargeDAO.getOneByuserId(userId);
	}
	
	public boolean save(SmsCharge smsCharge) {
		SmsCharge entity = smsChargeDAO.getOneByuserId(smsCharge.getUserId());
		int ret = 0;

		if (entity != null) {
			smsCharge.setId(entity.getId());
			smsCharge.setTotalMoney(entity.getTotalMoney()+smsCharge.getTotalMoney());
			smsCharge.setUsedMoney(entity.getUsedMoney()+smsCharge.getUsedMoney());
			smsCharge.setTotalSms(entity.getTotalSms()+smsCharge.getTotalSms());
			smsCharge.setUsedSms(entity.getUsedSms()+smsCharge.getUsedSms());
			ret = smsChargeDAO.updateById(smsCharge);
		} else {
			ret = smsChargeDAO.add(smsCharge);
		}
		return ret > 0 ? true : false;
	}
}
